package org.jeecg.modules.service.impl;




import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.entity.DbCusFollowUp;
import org.jeecg.modules.entity.DbCustomer;
import org.jeecg.modules.entity.vo.CusFollowUpVo;
import org.jeecg.modules.mapper.DbCusFollowUpMapper;
import org.jeecg.modules.service.IDbCusFollowUpService;
import org.jeecg.modules.service.IDbCustomerService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description: db_cus_follow_up
 * @Author: jeecg-boot
 * @Date:   2024-08-21
 * @Version: V1.0
 */
@Service
public class DbCusFollowUpServiceImpl extends ServiceImpl<DbCusFollowUpMapper, DbCusFollowUp> implements IDbCusFollowUpService {
    @Resource
    private IDbCustomerService dbCustomerService;
    @Resource
    private DbCusFollowUpMapper dbCusFollowUpMapper;
    @Override
    public List<CusFollowUpVo> getCusFollowOver(Integer day,String cusName) {
        LocalDate now = LocalDate.now();
        LocalDate date = now.minusDays(day);
//        查询回访信息
        List<DbCusFollowUp> dbCusFollowUpList = dbCusFollowUpMapper.listByLastDate(date,cusName);
        List<CusFollowUpVo> cusFollowUpVos = null;
        if(CollectionUtils.isNotEmpty(dbCusFollowUpList)){
            cusFollowUpVos = dbCusFollowUpList.stream().map(dbCusFollowUp -> CusFollowUpVo.builder()
                    .cusId(dbCusFollowUp.getCusId())
                    .cusName(dbCusFollowUp.getCusName())
                    .lastDate(dbCusFollowUp.getDate())
                    .content(dbCusFollowUp.getContent())
                    .build())
                    .collect(Collectors.toList());
        }
//        查询客户信息
        List<DbCustomer> dbCustomerList = dbCustomerService.list();
        Map<String, String> cusIdContactMap = dbCustomerList.stream().collect(Collectors.toMap(DbCustomer::getId, DbCustomer::getContact));
        Map<String, String> cusContactPhoneMap = dbCustomerList.stream().collect(Collectors.toMap(DbCustomer::getId, DbCustomer::getPhone));
        cusFollowUpVos.forEach(cusFollowUpVo -> {
            String cusId = cusFollowUpVo.getCusId();
            String contact = cusIdContactMap.get(cusId);
            String phone = cusContactPhoneMap.get(cusId);
            cusFollowUpVo.setContact(contact);
            cusFollowUpVo.setPhone(phone);
        });
        return cusFollowUpVos;
    }
}
